<?php
/**
 * Created by PhpStorm.
 * User: 86188
 * Date: 2021/2/2
 * Time: 8:51
 */

namespace app\jiangnan\controller;


use think\Controller;
use think\Db;
use think\Log;

class Carwelfare extends Controller
{
    protected $face_connnect=[
        // 数据库类型
        'type'        => 'mysql',
        // 服务器地址
        'hostname'    => '59.110.53.253',
        // 数据库名
        'database'    => 'jiangnandoor',
        // 数据库用户名
        'username'    => 'jiangnandoor',
        // 数据库密码
        'password'    => 'jiangnandoor',
        // 数据库编码默认采用utf8
        'charset'     => 'utf8',
    ];
    //访客车辆
    public function carlist(){


        $user_id=request()->get("user_id");
        if(strlen($user_id)!="16"){
            return "您还没有绑定房间";
        }
        $roominfo=Db::table("roominfo")->where("user_id",$user_id)->find();
        if(empty($roominfo)){
            $face_userinfo=Db::connect($this->face_connnect)->table("personnel")->where("user_id",$user_id)->find();
            $face_roominfo=Db::connect($this->face_connnect)->table("room")->where("id",$face_userinfo["room_id"])->find();
            $info["user_id"]=$user_id;
            $info["community_name"]="江南国际城";
            $info["group"]=$face_roominfo["group"];
            $info["building"]=$face_roominfo["building"];
            $info["unit"]=$face_roominfo["unit"];
            $info["room"]=$face_roominfo["room"];
            $info["phone"]=$face_roominfo["phone"];
            $info["owner_name"]=$face_roominfo["name"];
            $info["parkingName"]="江南国际园区停车";
            $info["parkingNo"]="1200018";
            $id=Db::table("roominfo")->insertGetId($info);
            $info["id"]=$id;
            $roominfo=$info;
        }


        $this->assign("user_id",$user_id);
        $this->assign("roominfo",$roominfo);
        $carlist=Db::table("car_welfare_owner")->where("room_id",$roominfo["id"])->select();
        $this->assign("carlist",$carlist);
        return $this->fetch();
    }

    public function caradd(){
        if(request()->isPost()){
            $data=request()->post();
            $roominfo=Db::table("roominfo")->where("user_id",$data["user_id"])->find();
            $carinfo=Db::table("car_welfare_owner")->where("parkingNo",$roominfo["parkingNo"])->where("car_number",$data["car_number"])->find();
            if(!empty($carinfo)){
                return json(['error' => '该车牌已经添加，不能重复添加']);
            }else{
                $info["parkingNo"]=$roominfo["parkingNo"];
                $info["parkingName"]=$roominfo["parkingName"];
                $info["owner_name"]=$roominfo["owner_name"];
                $info["owner_phone"]=$roominfo["phone"];
                $info["address"]=$roominfo["group"].$roominfo["building"]."幢".$roominfo["unit"]."单元".$roominfo["room"]."室";
                $info["create_time"]=time();
                $info["room_id"]=$roominfo["id"];

                $info["car_number"]=$data["car_number"];
                $info["operator"]=$data["user_id"];
                $info["name"]=$data["name"];
                $info["phone"]=$data["phone"];
                $res=Db::table("car_welfare_owner")->insert($info);
                if($res){
                    return json(['success' => '车牌添加成功']);
                }else{
                    return json(['error' => '车牌添加失败']);
                }
            }
        }else{
            $user_id=request()->get("user_id");
            $this->assign("user_id",$user_id);
            return $this->fetch();
        }
    }

    public function cardel(){
        $id=request()->post("id");
        $user_id=request()->post("user_id");
        $carinfo=Db::table("car_welfare_owner")
            ->where("id",$id)
            ->find();
        //Log::write($carinfo,"carinfo");

        if(empty($carinfo)){
            return json(['error' => '车辆信息不存在']);
        }
        $res=Db::table("car_welfare_owner")->where("id",$id)->delete();
        if($res){
            $data=$carinfo;
            $data["action"]=2;
            $data["operate_time"]=time();
            $data["operate_name"]=$user_id;
            Db::table("car_welfare_owner_operate")->insert($data);
            return json(['error' => '车辆删除成功']);
        }else{
            return json(['error' => '车辆信息不存在']);
        }
    }

    public function my(){
        $user_id=request()->get("user_id");
        if(strlen($user_id)!="16"){
            return "错误";
        }
        $this->assign("user_id",$user_id);
        $roominfo=Db::table("roominfo")->where("user_id",$user_id)->find();
        if(empty($roominfo)){
            return "错误";
        }
        $carlist=Db::table("car_welfare_owner")->where("room_id",$roominfo["id"])->select();
        $car_num=Db::table("car_welfare_owner")->where("room_id",$roominfo["id"])->count();
        $this->assign("carlist",$carlist);
        $this->assign("car_num",$car_num);

        //查询福利车订单
        $orderlist=Db::table("car_welfare_order")->where("room_id",$roominfo["id"])->order("id desc")->select();
        $this->assign("orderlist",$orderlist);

        //查询已经使用时长
        //根据房间查询福利车是否到期
        $year=date("Y");
        $month=date("m");
        $halfyear=1;
        if($month>6){
            $halfyear=2;
        }
        $timespan=Db::table("car_welfare_order")
            ->where("room_id",$roominfo["id"])
            ->where("in_year",$year)
            ->where("halfyear",$halfyear)
            ->where("pstatus",1)
            ->sum("fee_duration");
        $hours=round($timespan/3600,2);
        $lave_hours=30-$hours;
        $this->assign("hours",$hours);
        $this->assign("lave_hours",$lave_hours);
        return $this->fetch();
    }

}